#include "databacemanager.h"
#include <QSqlError>

DataBaceManager &DataBaceManager::instance()
{
    static DataBaceManager instance;
    return instance;
}

bool DataBaceManager::openDatabase(const QString &path)
{
    db.setDatabaseName(path);
    if (!db.open()) {
        qDebug() << "无法打开数据库：" << db.lastError().text();
        return false;
    }
    return true;
}

void DataBaceManager::closeDatabase()
{
    if(db.isOpen())
        db.close();
}

QString DataBaceManager::getDatabasePath() const
{
    return dbPath;
}

void DataBaceManager::setDatabasePath(const QString &path)
{
    if(path!=dbPath){
        dbPath=path;
        closeDatabase();
        openDatabase(path);
    }
}

DataBaceManager::~DataBaceManager()
{
    closeDatabase();
}

DataBaceManager::DataBaceManager(QObject *parent)
    : QObject{parent}
{
    db=QSqlDatabase::addDatabase("QSQLITE");
    openDatabase(dbPath);
}
